﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ReverseSentance
{
    

    class ReverseSentance
    {
        static StringBuilder sb = new StringBuilder();

        static string ReverseWords(Match match)
        {
            string pattern = @"\.|\?|\!";
            Regex regex = new Regex(pattern);

            if (regex.IsMatch(match.Value)==false)
            {
                sb.Insert(0, match.Value);
            }
            else
            {
                sb.Append(match.Value);
            }

            return match.Value;
        }

        static string Capitalize(string text)
        {
            string result = text.ToLower().TrimStart();
            result = Char.ToUpper(result[0]) + result.Substring(1);

            return result;
        }

        static void Main(string[] args)
        {
            string text = "Berries, picking is lass a.";
            string patternt = @"(\b(\w)+\b)|,|\.|\?|\!|\s";
            Regex.Replace(text, patternt, new MatchEvaluator(ReverseWords));
            string replaced; 
            replaced= sb.ToString().Replace(" ,", ", ");
            replaced = Capitalize(replaced);
            Console.WriteLine(replaced);
        }
    }
}
